import sys
import os
import time
import subprocess
from PyQt6.QtWidgets import (
    QApplication, QMainWindow, QSplitter, QTreeView, QTabWidget, QStatusBar, 
    QVBoxLayout, QWidget, QTextEdit, QGraphicsView, QGraphicsScene, QGraphicsRectItem,
    QToolBar, QMessageBox, QPushButton, QLabel, QMenu, 
    QInputDialog, QFileDialog, QDialog, QDialogButtonBox, QSizePolicy
)
from PyQt6.QtGui import (
    QIcon, QTextCharFormat, QColor, QFont, QPen, QBrush, QPainterPath, QFileSystemModel,
    QTextCursor, QPalette, QStandardItemModel, QStandardItem, QSyntaxHighlighter, 
    QRegularExpressionValidator, QAction, QPainter
)
from PyQt6.QtCore import (
    Qt, QRectF, QPointF, QModelIndex, QFileInfo, QSortFilterProxyModel,
    QRegularExpression, QTimer, QSize
)
import git
from git import Repo, GitCommandError, InvalidGitRepositoryError






# ===================== 代码编辑器和语法高亮 =====================
class PythonHighlighter(QSyntaxHighlighter):
    def __init__(self, document):
        super().__init__(document)
        self.rules = []
        
        # 设置高亮规则
        self.init_highlight_rules()
    
    def init_highlight_rules(self):
        # 关键字
        keywords = [
            "and", "as", "assert", "break", "class", "continue", 
            "def", "del", "elif", "else", "except", "finally", 
            "for", "from", "global", "if", "import", "in", "is", 
            "lambda", "nonlocal", "not", "or", "pass", "raise", 
            "return", "try", "while", "with", "yield", "None", 
            "True", "False"
        ]
        keyword_format = QTextCharFormat()
        keyword_format.setForeground(QColor("#569cd6"))  # 蓝色
        keyword_format.setFontWeight(QFont.Weight.Bold)
        self.rules.extend([(r'\b%s\b' % kw, keyword_format) for kw in keywords])
        
        # 类名
        class_format = QTextCharFormat()
        class_format.setForeground(QColor("#4ec9b0"))  # 青色
        class_format.setFontWeight(QFont.Weight.Bold)
        self.rules.append((r'\b[A-Z]\w*\b', class_format))
        
        # 字符串
        string_format = QTextCharFormat()
        string_format.setForeground(QColor("#d69d85"))  # 橙色
        self.rules.extend([
            ('".*?"', string_format),
            ("'.*?'", string_format)
        ])
        
        # 注释
        comment_format = QTextCharFormat()
        comment_format.setForeground(QColor("#57a64a"))  # 绿色
        self.rules.append(('#.*', comment_format))
        
        # 数字
        number_format = QTextCharFormat()
        number_format.setForeground(QColor("#b5cea8"))  # 浅绿色
        self.rules.append(('\\b[0-9]+\\b', number_format))
        
        # 函数名
        function_format = QTextCharFormat()
        function_format.setForeground(QColor("#dcdcaa"))  # 黄色
        self.rules.append(('\\b[a-zA-Z_][a-zA-Z0-9_]*(?=\\()', function_format))
    
    def highlightBlock(self, text):
        for pattern, format in self.rules:
            regex = QRegularExpression(pattern)
            match_iter = regex.globalMatch(text)
            while match_iter.hasNext():
                match = match_iter.next()
                self.setFormat(match.capturedStart(), match.capturedLength(), format)

